package br.com.mocamorena.view;

import java.awt.AWTKeyStroke;
import java.awt.Component;
import java.awt.Container;
import java.awt.KeyboardFocusManager;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.util.HashSet;
import java.util.Set;
import javax.swing.JComboBox;
import javax.swing.text.JTextComponent;
import org.jdesktop.swingx.JXDatePicker;

public final class UtilitiesGUI {

    private UtilitiesGUI() {
    }

    public static void respondToArrowEnterKey(Component c) {
        Set<AWTKeyStroke> newKeystrokes;

        newKeystrokes = new HashSet(c.getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS));
        newKeystrokes.add(AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_ENTER, 0));

        if (!(c instanceof JComboBox)) {
            newKeystrokes.add(AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_DOWN, 0));
            newKeystrokes.add(AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_KP_DOWN, 0));
        }

        c.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, newKeystrokes);

        newKeystrokes = new HashSet(c.getFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS));
        newKeystrokes.add(AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_ENTER, InputEvent.SHIFT_DOWN_MASK));

        if (!(c instanceof JComboBox)) {
            newKeystrokes.add(AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_UP, 0));
            newKeystrokes.add(AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_KP_UP, 0));
        }

        c.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, newKeystrokes);
    }

    public static void respondToEnterKey(Component c) {
        Set<AWTKeyStroke> newKeystrokes;

        newKeystrokes = new HashSet(c.getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS));
        newKeystrokes.add(AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_ENTER, 0));

        c.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, newKeystrokes);

        newKeystrokes = new HashSet(c.getFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS));
        newKeystrokes.add(AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_ENTER, InputEvent.SHIFT_DOWN_MASK));

        c.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, newKeystrokes);
    }

    public static void selectAllValuesInField(java.awt.event.FocusEvent evt) {
        if (evt == null) {
            return;
        }

        javax.swing.JTextField field = (javax.swing.JTextField) evt.getComponent();
        field.setCaretPosition(0);

        if (field.getText() != null) {
            field.moveCaretPosition(field.getText().length());
        }
    }
}